package com.cpi.cornpp.costelement.action;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.costelement.exception.CostElementException;
import com.cpi.cornpp.costelement.form.CostElementForm;
import com.cpi.cornpp.costelement.services.CostElementServices;
import com.cpi.cornpp.costelement.view.CostElementView;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportChargeView;
import com.cpi.cornpp.transports.view.TransportView;

public class SaveEditCostElementAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws CostElementException{
		CostElementForm costElementForm = null;
		CostElementView costElementView = null;
		TransportChargeView transportChargeView = null;
		TransportView transportView = null;
		CostElementServices costElementService = null;
		TransportService transportService = null;
		List deletedTransports = null;
		List addedTransports = null;
		HttpSession session = null;
		ActionForward forward = null;
		String messageError = null;
		MessageResources messageResources = null;
		
		session = request.getSession();
		messageResources = getResources(request);
		forward = mapping.findForward(FAILURE_FORWARD);
		
		deletedTransports = (List) session.getAttribute("deletedCostElementTransport");
		addedTransports = (List) session.getAttribute("addedCostElementTransport");
		
		try{
			
			if (form != null){
				costElementForm = (CostElementForm) form;

				costElementView = new CostElementView();
				costElementService = new CostElementServices();
				transportService = new TransportService();
				
				costElementView.setCostElementID(costElementForm.getCostElementView().getCostElementID());
				costElementView.setCostElementCode(costElementForm.getCostElementView().getCostElementCode());
				costElementView.setCostElementDescription(costElementForm.getCostElementView().getCostElementDescription());
				
				costElementService.updateCostElement(costElementView);
				
				if (CollectionUtils.isNotEmpty(deletedTransports)){
					
					for (Iterator itr = deletedTransports.iterator(); itr.hasNext(); ){
						transportChargeView = new TransportChargeView();
						transportView = (TransportView) itr.next();
						
						transportChargeView.setTransportChargeTransportId(transportView.getTransportIdentifier());
						transportChargeView.setTransportChargeTypeId(costElementView.getCostElementID());
						
						transportService.removeTransportCharge(transportChargeView);
					}
				}
				
				if (CollectionUtils.isNotEmpty(addedTransports)){
					
					for (Iterator itr = addedTransports.iterator(); itr.hasNext(); ){
						transportChargeView = new TransportChargeView();
						transportView = (TransportView) itr.next();
						
						transportChargeView.setTransportChargeTransportId(transportView.getTransportIdentifier());
						transportChargeView.setTransportChargeTypeId(costElementView.getCostElementID());
						transportChargeView.setTransportChargeAmount(transportView.getTransportCost());
						
						transportService.addTransportCharge(transportChargeView);
					}
				}
				
				messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
				session.setAttribute(MESSAGE, messageError);
				
				forward = mapping.findForward(SUCCESS_FORWARD);
			}
			
		} catch(CostElementException cee){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), cee.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, cee);
				form.reset(mapping, request);
				throw cee;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
		} catch(Exception e){
			CostElementException cee = new CostElementException(e , CostElementException.LAYER_ACTION, CostElementException.ACTION_INSERT);
			
			session.setAttribute(CUSTOM_EXCEPTION, cee);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw cee;
		} finally {
			session.removeAttribute("costElementView");
			session.removeAttribute("deletedCostElementTransport");
			session.removeAttribute("addedCostElementsTransport");
		}
		
		return forward;
	}
}